package com.tanhua.controller;


import com.tanhua.domain.db.User;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.service.UserInfoService;
import com.tanhua.service.UserService;
import com.tanhua.util.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 登录
     */
    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map map){
        String mobile = (String) map.get("phone");
        return userService.sendSms(mobile);

    }

    /**
     * 验证
     */
    @RequestMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map map){
        String phone = (String) map.get("phone");
        String code = (String) map.get("verificationCode");
        return userService.loginVerification(phone,code);

    }

    /**
     * 存储个人信息
     */
    @RequestMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo,
                                       @RequestHeader("Authorization")String token){

        User user = UserHolder.getUser();
        System.out.println(user.getId());
        userInfo.setId(user.getId());
        return userInfoService.save(userInfo);
    }
    /**
     * 上传头像
     * /user/loginReginfo/head
     */
    @RequestMapping("/loginReginfo/head")
    public ResponseEntity upHeader(MultipartFile headPhoto,
                                   @RequestHeader("Authorization") String token) throws IOException {
        //User user = userInfoService.findUserByToken(token);
        User user = UserHolder.getUser();
        return userInfoService.saveHeader(headPhoto,user.getId());
    }
    /**
     *
     */
}
